package com.itboye.ihomebank;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.IYWUIPushListener;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.itboye.ihomebank.activity.login.LoginActivity;
import com.itboye.ihomebank.activity.message.ActivitySystemMessage;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.bean.MessWeiDuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 消息
 */
public class MessageFragment extends BaseFragment implements OnClickListener, Observer {
    View v_statusbar;
    TextView add_shap_title_tv;
    ImageView close_icon, img_other;

    RelativeLayout contacts_system, contacts_qianyue, contacts_yuyue,
            contacts_mensuo ,contacts_elemeter;
    private String uid;

    private TextView contacts_xitong, contacts_qy, contacts_yy, contacts_ms,contract_elem;//显示消息的标签

    KeyPresenter keyPresenter;
    MessWeiDuBean bean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.contacts_layout, null);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public int initView() {
        return 0;
    }

    @Override
    public void onMyActivityCreated() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("消息");
        close_icon.setVisibility(View.GONE);
        img_other.setVisibility(View.GONE);

        contacts_xitong.setVisibility(View.GONE);
        contacts_qy.setVisibility(View.GONE);
        contacts_yy.setVisibility(View.GONE);
        contacts_ms.setVisibility(View.GONE);

        keyPresenter = new KeyPresenter(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
        if (!uid.equals("")) {
            keyPresenter.getWeiDu(uid);
        }
    }

    // /消息类型【1=>系统消息，2=>预约消息,3=>签约消息,4=>智能锁消息】
    @Override
    public void onClick(View v) {
        Intent intent = null;
        uid = SPUtils.get(getActivity(), null, SPContants.USER_ID, "") + "";
        if (uid.equals("")) {
            contacts_xitong.setVisibility(View.GONE);
            contacts_qy.setVisibility(View.GONE);
            contacts_yy.setVisibility(View.GONE);
            contacts_ms.setVisibility(View.GONE);
            contract_elem.setVisibility(View.GONE);
            intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);

            return;
        }
        switch (v.getId()) {
            case R.id.contacts_system:// 系统消息
                intent = new Intent(getActivity(), ActivitySystemMessage.class);
                intent.putExtra("type", 1);
                intent.putExtra("title", "系统消息");
                startActivity(intent);
                break;
            case R.id.contacts_qianyue:// 签约消息
                intent = new Intent(getActivity(), ActivitySystemMessage.class);
                intent.putExtra("type", 3);
                intent.putExtra("title", "签约消息");
                startActivity(intent);
                break;
            case R.id.contacts_mensuo:// 门锁消息
                intent = new Intent(getActivity(), ActivitySystemMessage.class);
                intent.putExtra("type", 4);
                intent.putExtra("title", "门锁消息");
                startActivity(intent);
                break;
            case R.id.contacts_elemeter: //电表消息
                intent = new Intent(getActivity(), ActivitySystemMessage.class);
                intent.putExtra("type", 5);
                intent.putExtra("title", "电表消息");
                startActivity(intent);
                break;
            case R.id.contacts_yuyue:
                // 此实现不一定要放在Application onCreate中
                final String userid = SPUtils.get(getActivity(), null,
                        SPContants.BaichuanUID, "") + "";
                // final String userid =
                // "tbimuser_8f85517967795eeef66c225f7883bdcb";
                // 此对象获取到后，保存为全局对象，供APP使用
                // 此对象跟用户相关，如果切换了用户，需要重新获取
                final YWIMKit mIMKit = YWAPI.getIMKitInstance(userid,
                        SPContants.YW_APPKEY);
                mIMKit.unRegisterPushListener(new IYWUIPushListener() {

                    @Override
                    public void onMessageComing() {
                        // TODO Auto-generated method stub
                    }
                });
                // 开始登录
                String password = "itboye";
                IYWLoginService loginService = mIMKit.getLoginService();
                YWLoginParam loginParam = YWLoginParam.createLoginParam(userid,
                        password);
                loginService.login(loginParam, new IWxCallback() {

                    @Override
                    public void onSuccess(Object... arg0) {
                        Intent intent = mIMKit.getConversationActivityIntent();// 获取会话列表
                        startActivity(intent);
                    }

                    @Override
                    public void onProgress(int arg0) {
                        // TODO Auto-generated method stub
                    }
                    @Override
                    public void onError(int errCode, String description) {
                        ByAlert.alert(errCode + ":" + description);// 如果登录失败，errCode为错误码,description是错误的具体描述信息
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        // ByAlert.alert(entity.getData());
        if (entity != null) {
            if (entity.getEventType() == KeyPresenter.weidu_success) {
                bean = (MessWeiDuBean) entity.getData();
                if (bean.getSystem() > 0) {
                    contacts_xitong.setVisibility(View.VISIBLE);
                    contacts_xitong.setText(bean.getSystem() + "");
                }else{
                    contacts_xitong.setVisibility(View.GONE);
                }
                if (bean.getSign() > 0) {
                    contacts_qy.setVisibility(View.VISIBLE);
                    contacts_qy.setText(bean.getSign() + "");
                }else{
                    contacts_qy.setVisibility(View.GONE);
                }

                if (bean.getLock() > 0) {
                    contacts_ms.setVisibility(View.VISIBLE);
                    contacts_ms.setText(bean.getLock() + "");
                }else{
                    contacts_ms.setVisibility(View.GONE);
                }
                if (bean.getEmeter()>0){
                    contract_elem.setVisibility(View.VISIBLE);
                    contract_elem.setText(bean.getEmeter()+ "");
                }else {
                    contract_elem.setVisibility(View.GONE);
                }
                // 有新消息  发送广播
                Intent intent2 = new Intent("mess_new");
                getActivity().sendBroadcast(intent2);
                if (bean.getSystem() > 0||bean.getSign() > 0||bean.getLock() > 0){
                    SPUtils.put(getActivity(), null,
                            SPContants.MESS_NEW, true);
                }else{
                    SPUtils.put(getActivity(), null,
                            SPContants.MESS_NEW, false);
                }
            }
        }

    }

    //退出登录时调用
    public void hideRedDot(){
        contacts_xitong.setVisibility(View.GONE);
        contacts_qy.setVisibility(View.GONE);
        contacts_yy.setVisibility(View.GONE);
        contacts_ms.setVisibility(View.GONE);
    }
    //登录时调用
    public void showRedDot(){
        //系统
        if (((Integer)SPUtils.get(getActivity(),null,SPContants.SYSTEM_MESSAGE,0))>0){
            contacts_xitong.setVisibility(View.VISIBLE);
            contacts_xitong.setText(SPUtils.get(getActivity(),null,SPContants.SYSTEM_MESSAGE,0)+"");
        }

        //签约
        if (((Integer)SPUtils.get(getActivity(),null,SPContants.CONTRANCT_MESSAGE,0))>0){
            contacts_qy.setVisibility(View.VISIBLE);
            contacts_qy.setText(SPUtils.get(getActivity(),null,SPContants.CONTRANCT_MESSAGE,0)+"");
        }

        //锁
        if (((Integer)SPUtils.get(getActivity(),null,SPContants.LOCK_MESSAGE,0))>0){
            contacts_ms.setVisibility(View.VISIBLE);
            contacts_ms.setText(SPUtils.get(getActivity(),null,SPContants.LOCK_MESSAGE,0)+"");
        }
    }
}
